//
//  MBShowForICell.m
//  MiBaoTu
//
//  Created by mac004 on 2016/10/11.
//  Copyright © 2016年 com.caesartech15. All rights reserved.
//

#import "MBShowForICell.h"

@implementation MBShowForICell

-(instancetype)initWithFrame:(CGRect)frame{
    if (self=[super initWithFrame:frame]) {
        [self onCreatUI];
    }
    return self;
}
-(void)onCreatUI{
    self.backgroundColor=[UIColor whiteColor];
    
    self.iconBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    self.iconBtn.frame=CGRectMake(30*PERCEN, 15*PERCEN, 100*PERCEN, 100*PERCEN);
    [self.iconBtn addTarget:self action:@selector(onSelectIconBtnWithView) forControlEvents:UIControlEventTouchUpInside];
    self.img=[[UIImageView alloc]initWithFrame:self.iconBtn.frame];
    self.img.layer.cornerRadius=self.img.height/2;
    self.img.layer.masksToBounds=YES;
    [self.iconBtn addSubview:self.img];
    [self.contentView addSubview:self.iconBtn];
    
    self.title=[[UILabel alloc]initWithFrame:CGRectMake(172*PERCEN, 40*PERCEN, 300*PERCEN, 40*PERCEN)];
    self.title.font=[UIFont systemFontOfSize:30*PERCEN];
    self.title.textColor=UIColorFromHex(0x333333);
    [self.contentView addSubview:self.title];
    
    self.numberOfZan=[[UILabel alloc]initWithFrame:CGRectMake(self.width-110*PERCEN, self.title.top, 80*PERCEN, self.title.height)];
    self.numberOfZan.textColor=UIColorFromHex(0x333333);
    self.numberOfZan.font=[UIFont systemFontOfSize:30*PERCEN];
    self.numberOfZan.textAlignment=NSTextAlignmentRight;
    
    UITapGestureRecognizer*tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onSelectPraiseAction)];
    [self.numberOfZan addGestureRecognizer:tap];
    self.numberOfZan.userInteractionEnabled=YES;
   // self.numberOfZan.text=@"赞(0)";
    [self.contentView addSubview:self.numberOfZan];
    
    self.zan=[UIButton buttonWithType:UIButtonTypeCustom];
    self.zan.frame=CGRectMake(self.numberOfZan.left-40*PERCEN, self.title.top+5*PERCEN, 30*PERCEN, 30*PERCEN);
    [self.zan addTarget:self action:@selector(onSelectPraiseAction) forControlEvents:UIControlEventTouchUpInside];
    //[self.zan setBackgroundImage:[UIImage imageNamed:@"zan1"] forState:UIControlStateNormal];
    [self.contentView addSubview:self.zan];
    
    UIView*view=[[UIView alloc]initWithFrame:CGRectMake(172*PERCEN, self.zan.bottom+36*PERCEN, self.width/982*760, self.width/982*618)];
    view.backgroundColor=UIColorFromHex(0xf5f5f5) ;
    [self.contentView addSubview:view];
    
    self.bigImageView=[[UIImageView alloc]initWithFrame:CGRectMake(30*PERCEN, 30*PERCEN, view.width-60*PERCEN, (view.width-60*PERCEN)/700*510)];
    [view addSubview:self.bigImageView];
    
    self.themeTitle=[[UILabel alloc]initWithFrame:CGRectMake(30*PERCEN, self.bigImageView.bottom+20*PERCEN, 300*PERCEN, 30*PERCEN)];
    self.themeTitle.font=[UIFont systemFontOfSize:30*PERCEN];
    self.themeTitle.textColor=UIColorFromHex(0x33333);
    [view addSubview: self.themeTitle];
    
    self.style=[[UILabel alloc]initWithFrame:CGRectMake(view.width-110*PERCEN, self.themeTitle.top, 100*PERCEN, 30*PERCEN)];
    self.style.font=[UIFont systemFontOfSize:28*PERCEN];
    self.style.textColor=UIColorFromHex(0x999999);
   // self.style.text=@"主播秀";
    [view addSubview:self.style];
    
    UIImageView*imgView=[[UIImageView alloc]initWithFrame:CGRectMake(self.style.left-50*PERCEN, self.style.top, 40*PERCEN, 40*PERCEN)];
    imgView.image=[UIImage imageNamed:@"biaoqian"];
    [view addSubview:imgView];
    
    
    
}

-(void)setDataDic:(NSDictionary *)dataDic{
    
    
    _dataDic=dataDic;

    _temportrailyDic=[NSMutableDictionary dictionaryWithDictionary:dataDic];
//    NSMutableArray*praiseArr=[UserManager standerDefult].praiseListArr;
    
    
//    for (NSDictionary*dict in praiseArr) {
//        if ([dict[@"PraisedID"]isEqual:dataDic[@"ID"]]&& [dict[@"PraisedType"] isEqual: @1]) {
//            self.isPraise=YES;
//            
//            }
//        else{
//           self.isPraise=NO;
//        }
//    }
 
 
    
    if ([dataDic[@"FaceImg"] isKindOfClass:[NSNull class]]) {
        self.img.image=[UIImage imageNamed:@"touxiang_3.0"];
    }else{
    [self.img sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",MUSIC_PLAY_Url,dataDic[@"FaceImg"]]] placeholderImage:[UIImage imageNamed:@"touxiang_3.0"]];
    }
    self.title.text=dataDic[@"NickName"];
    self.themeTitle.text=dataDic[@"Sentiment"];
    NSString*type=[NSString stringWithFormat:@"%@",dataDic[@"ShowType"]];
    if ([type isEqualToString:@"1"]) {
        self.style.text=@"宝贝秀";
    }else{
        self.style.text=@"主播秀";
    }
    if ([dataDic[@"CoverPath"]  isKindOfClass:[NSNull class]]) {
        self.bigImageView.image=[UIImage imageNamed:@"morenfm2_qzx_3.0"];
    }else{
        [self.bigImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",MUSIC_PLAY_Url,dataDic[@"CoverPath"]]] placeholderImage:[UIImage imageNamed:@"morenfm2_qzx_3.0"] options:SDWebImageRetryFailed completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            _bigImageView.image = [self getCenterImg:image];
        }];

//        
//    [self.bigImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",MUSIC_PLAY_Url,dataDic[@"CoverPath"]] ] placeholderImage:[UIImage imageNamed:@"morenfm2_qzx_3.0"]];
    }
    
    NSString*numText=[NSString stringWithFormat:@"赞 (%@)",dataDic[@"PraiseNum"] ];
    
    float width=[numText boundingRectWithSize:CGSizeMake(1000, 30*PERCEN) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:self.numberOfZan.font} context:nil].size.width+5;
    self.numberOfZan.text=numText;
    
    self.numberOfZan.frame=CGRectMake(self.width-50*PERCEN-width, self.title.top, width, 30*PERCEN);
     self.zan.frame=CGRectMake(self.numberOfZan.left-40*PERCEN, self.title.top, 30*PERCEN, 30*PERCEN);
  //  self.numberOfZan.text=[NSString stringWithFormat:@"赞 (%@)",dataDic[@"PraiseNum"] ];
       _PraiseNum=[dataDic[@"PraiseNum"]integerValue];
    
    
    NSString*PraiseStatus=[NSString stringWithFormat:@"%@",dataDic[@"PraiseStatus"]];
    if ([PraiseStatus isEqualToString:@"1"]) {
        [self.zan setImage:[UIImage imageNamed:@"zan2"] forState:0];
        self.numberOfZan.textColor=TINK_COLOR;
    }else{
        [self.zan setImage:[UIImage imageNamed:@"zan1"] forState:0];
         self.title.textColor=UIColorFromHex(0x333333);
    }
    
    
//    if (_isPraise) {
//        [self.zan setImage:[UIImage imageNamed:@"zan2"] forState:0];
//    }
//    else{
//        [self.zan setImage:[UIImage imageNamed:@"zan1"] forState:0];
//    }

    
    
    
}
#pragma mark--修正图片

- (UIImage *)getCenterImg:(UIImage *)image
{
    CGImageRef imageRef = image.CGImage;
    float imgHeight = 1.0 * image.size.width * _bigImageView.height/_bigImageView.width;
    float rectHeight = imgHeight;
    float top = (image.size.height-imgHeight)/2.;
    CGRect rect;
    if (top > 0) {
        //图片实际高度比UI高度高
        if (imgHeight > image.size.height) {
            rectHeight = image.size.height;
            top = (imgHeight - image.size.height)/2.;
        }
        rect = CGRectMake(0, top, image.size.width, rectHeight);
    }
    else {
        //UI高度比图片实际高度高
        float imgWidth = 1.0 * image.size.height * _bigImageView.width/_bigImageView.height;
        float rectWidth = imgWidth;
        float left = (image.size.width-imgWidth)/2.;
        if (imgWidth > image.size.width) {
            rectWidth = image.size.width;
            left = (imgWidth - image.size.width)/2.;
        }
        rect = CGRectMake(left, 0, rectWidth, image.size.height);
    }
    CGImageRef imagePartRef = CGImageCreateWithImageInRect(imageRef, rect);
    UIImage *rectImg = [UIImage imageWithCGImage:imagePartRef];
    CGImageRelease(imagePartRef);
    return rectImg;
}
-(void)setIsPraise:(BOOL)isPraise{
    _isPraise=isPraise;
    if (isPraise) {
        [self.zan setImage:[UIImage imageNamed:@"zan2"] forState:0];
        self.numberOfZan.textColor=TINK_COLOR;
    }
    else{
        [self.zan setImage:[UIImage imageNamed:@"zan1"] forState:0];
         self.title.textColor=UIColorFromHex(0x333333);
    }

}
-(void)setPraiseNum:(NSInteger )PraiseNum{
    _PraiseNum=PraiseNum;
    self.numberOfZan.text=[NSString stringWithFormat:@"赞 (%d)",PraiseNum];
}




-(void)onSelectIconBtnWithView {
    if (_delegate && [_delegate respondsToSelector:@selector(onSelectIconBtnWithView:)]) {
        [_delegate onSelectIconBtnWithView:self];
    }
}
-(void)onSelectPraiseAction {
    if (_delegate && [_delegate respondsToSelector:@selector(onSelectPraiseBtnWithView:withValue:)]) {
        [_delegate onSelectPraiseBtnWithView:self withValue:_dataDic];
    }
    
}

//- (void)setIsZan:(BOOL)isZan
//{
//    _isZan = isZan;
//    [self setNeedsLayout];
//}

- (void)onUpdateZanBtAction:(BOOL)isZan
{
    _isZan = isZan;
    if (_isZan) {
        [self.zan setImage:[UIImage imageNamed:@"zan2"] forState:0];
         self.numberOfZan.textColor=TINK_COLOR;
    }
    else{
        self.numberOfZan.textColor = UIColorFromHex(0x333333);
        [self.zan setImage:[UIImage imageNamed:@"zan1"] forState:0];
    }

}

@end
